#!/usr/bin/python
# -*- coding: utf-8 -*-

from ini import *
from utils import *

def init(bot):
	bot.addCommand("watch", cmd_watch)

def unload(bot):
	bot.removeCommand('watch')

def cmd_watch(bot, user, args):
	"""Get certain users' status changes.
	Usage: )watch [<command>] <user name>

	Commands:
	<blank> - Add a user to your list.
	del - Remove a user from your list.

	Note: Type )watch to see whos on your list."""

	user = getjid(user)
	if not args or args == "list":
		#This is the default message.
		list = "You have no users in your notification list."
		sort = []
		for name, i in database["watched"].items():
			name = getdisplayname(name)
			if user in i:
				list = "These are the users you have been asked to be notified about:\n\n"
				sort.append(name)

		#Sort the names in alphabetical order.
		if sort != []:
			sort.sort()
			for name in sort:
				list += name + '\n'

		bot.msg(user, list)
		return

	args = args.lower()
	if ' ' not in args:
		jid = getjid(args)
		add_dbentry("watched", jid, user)
		bot.msg(user, "%s has been added to your watch list." % getdisplayname(jid))
		return

	cmd, jid = args.split(' ',1)
	if ' ' in jid:
		jid = jid[:jid.find(' ')]
	jid = getjid(jid)
	cmd = cmd.lower()

	if cmd == "add":
		add_dbentry("watched", jid, user)
		bot.msg(user, "%s has been added to your watch list." % getdisplayname(jid))

	else:
		del_dbentry("watched", jid, user)
		bot.msg(user, "%s has been removed your watch list." % getdisplayname(jid))
